<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page import="com.broadvision.data.client.DataManager" %>
<%@ page import="com.broadvision.emarketing.action.segment.SegmentConstants" %>

<%--
------------------------------Note----------------------------
In order to include this page, you need have a request attribute 
with name: categoryList, which represents the category list.
--%>

<c:set var="viewBean" value="${emSegmentBrowseView}" />

<c:set var="editIcon">
  <html:img   page="/mc/images/action/icon_edit.gif" 
    width="15" height="15" border="0" styleClass="actionIcon"
    altKey="em.image.alt.edit" titleKey="em.image.alt.edit" bundle="em"/>
</c:set>
<c:set var="deleteIcon">
  <html:img page="/mc/images/action/icon_delete.gif"
    width="15" height="15" border="0" styleClass="actionIcon"
    altKey="em.image.alt.delete" titleKey="em.image.alt.delete" bundle="em"/>
</c:set>



<Script language="JavaScript">

  function check(field, button) {
    var SelectAllTxt = '<bean:message key="button.selectAll" bundle="mc.messages"/>';
    var UnselectAllTxt = '<bean:message key="button.unselectAll" bundle="em"/>';
    if (field == null) {
      button.value = SelectAllTxt;
    }
    var checkflag = "false";
    if (button.value == SelectAllTxt) {
      checkflag = "false";
    }
    if (button.value == UnselectAllTxt) {
      checkflag = "true";
    }
    var max = field.length;
    if (max > 1) {
      if (checkflag == "false") {
        for (i = 0; i < max; i++) {
          field[i].checked = true;
        }
        button.value = UnselectAllTxt;
      } else {
        for (i = 0; i < max; i++) {
          field[i].checked = false;
        }
        button.value = SelectAllTxt;
      }
    } else {
      if (checkflag == "false") {
        field.checked = true;
        button.value = UnselectAllTxt;
      } else {
        field.checked = false;
        button.value = SelectAllTxt;
      }
    }
  }


  // function to POST form parameters to a ".do"
  function doCatDelAction(action, myform) {
    var count = 0;

    if ( myform.categoryOid == null ) 
    {
      return false;
    }

    if ( myform.categoryOid.length == null ) 
    {
      if ( myform.categoryOid.checked == true ) count = 1;
    } else {
      for (i = 0; i < myform.categoryOid.length; i++) {
        if ( myform.categoryOid[i].checked == true ) count = count + 1;
      }
    }
    document.emSegmentCategoryMoveDeleteForm.action = '<html:rewrite page="'+action+'" />';   
    document.emSegmentCategoryMoveDeleteForm.submit();
    return false;
  }


  function validateCategoryFormBeforeDelete(myform)
  {
    var count = 0;

    if ( myform.categoryOid != null ) 
    {
      if ( myform.categoryOid.length == null ) 
      {
        if ( myform.categoryOid.checked == true ) {
          count = 1;
        }
      }

      for (i = 0; i < myform.categoryOid.length; i++) {
        if ( myform.categoryOid[i].checked == true )count++;
      }
    }

    return confirmCategoryDelete(count);
  }

  function confirmCategoryDelete(count) {
    if (count == 0) {
      alert( "<bean-el:message bundle="em" key="em.segment.browse.js.alert.selectSegmentCategory"/>")
      return false;
    }

    if (count == 1)
      return confirm("<bean-el:message bundle="em" key="em.segment.browse.js.alert.deleteCategory"/>");
    else
      return confirm("<bean-el:message bundle="em" key="em.segment.browse.js.alert.deleteCategories"/>");
  }

  function doCategoryMoveAction(myform)
  {
    var count = 0;
    if (myform.categoryOid == null) {
      //No category has been selected.
      return false;
    }

    //There is at least one segment in this category.
    if ( myform.categoryOid.length == null ) {
      //There is only one category selected.
      if ( myform.categoryOid.checked == true ) count++;
    } else {
      for (i = 0; i < myform.categoryOid.length; i++) {
        if ( myform.categoryOid[i].checked == true ) count++;
      }
    }

    if (count == 0) {
      //The user doesn't select any segment
      alert( "<bean:message bundle="em" key="em.segment.browse.js.alert.selectSegmentCategory"/>")
      return false;
    } else {
      //The user selects at least one segment.
      <%
        final int categoryCntTypeId =
          DataManager.getContentTypeId(SegmentConstants.SEGMENT_CONTENT_TYPE_NAME);
      %>
      doPickerPopupAction('<html:rewrite page="/mt/categoryPickerPopup.do"/>', 'callBackForCategoryMove', <%=categoryCntTypeId%>, '<c:out value="${emSegmentBrowseForm.map.currentCategoryOid}" />', null, true, false, false);
      return false;
    }
  }

  function callBackForCategoryMove(catId, catFullPath) {
    // Add code according to your need
    var theForm = document.emSegmentCategoryMoveDeleteForm;

    theForm.destinationCategoryOid.value = catId;
    theForm.action = '<html:rewrite page="/em/segment/segmentCategoryMove.do" />';
    theForm.submit();
  }


</Script>

<html:form action="/em/segment/segmentCategoryDelete.do">
  <html:hidden property="destinationCategoryOid"/>
  <c:if test="${not empty categoryList}">
    <div class="globalButtonWrap">

      <input type="button" name="selectAll" class="globalButton" 
        onClick="return check(this.form.categoryOid, this);"
        value="<bean-el:message bundle="em" key="button.selectAll"/>">
      <input type="button" name="deleteBtn" class="globalButton"
        onClick="if( validateCategoryFormBeforeDelete(this.form)) doCatDelAction('/em/segment/segmentCategoryDelete.do', this.form);"
        value="<bean-el:message bundle="em" key="button.delete"/>">
      <input type="button" name="moveBtn" class="globalButton"
        onClick="return doCategoryMoveAction(this.form);"
        value="<bean-el:message bundle="em" key="button.move"/>">
    </div>
  </c:if>

  <table class="dataTable" border="0" cellspacing="1" cellpadding="2" width="100%">
    <tr class="listHeader">
      <th id="Checkboxes" width="5%">&nbsp;</th>
      <th id="Name" align="left" width="30%">
        <bean-el:message bundle="mc.messages" key="mc.header.name"/>
      </th>
      <th id="CreationTime" align="left" width="25%">
        <bean-el:message key="em.segment.creation.date" bundle="em"/>
      </th>
      <th id="LastModified" width="25%" align="left">
        <bean-el:message bundle="mc.messages" key="mc.header.lastModifiedDate"/>
      </th>
      <th id="Actions" align="left" width="15%" nowrap>
        <bean-el:message bundle="mc.messages" key="mc.header.actions"/>
      </th>
    </tr>
    <!-- End Category list header row -->
  
    <!-- Start category list row -->
    <c:if test="${empty categoryList}">
      <tr class="listEven">
        <td colspan="5" align="center">
          <bean-el:message key="em.segment.browse.no.category" bundle="em" />
        </td>
      </tr>
    </c:if>

    <c:forEach var="category" items="${categoryList}" varStatus="index">
      <c:set var="rowStyle">
        <c:choose>
          <c:when test="${index.count % 2 == 0}">
            listEven
          </c:when>
          <c:otherwise>
            listOdd
          </c:otherwise>
        </c:choose>
      </c:set>
      <tr class="<c:out value="${rowStyle}" />" >
        <td id="Checkboxes" width="5%">
          <input type="checkbox" name="categoryOid" 
            value="<c:out value="${category.map.oid}"/>">
        </td>
        <td id="Name" align="left" width="30%">
          <c:url var="browseUrl" value="/em/segment/segmentBrowse.do">
            <c:param name="currentCategoryOid" value="${category.map.oid}"/>
          </c:url>
          <a href="<c:out value="${browseUrl}" />"><c:out value="${category.map.name}" /></a>
        </td>
        <td id="id="CreationTime" align="left" width="25%">
          <bean:write name="category" property="created" bundle="mc.messages" formatKey="format.date.medium"/>
        </td>
        <td id="LastModified" width="25%" align="left">
          <bean:write name="category" property="lastModified" bundle="mc.messages" formatKey="format.date.medium"/>
        </td>
        <td id="Actions" align="left" width="15%" nowrap>
          <html:link paramName="category"  paramProperty="oid"
             paramId="categoryOid" page="/em/segment/segmentCategoryEdit.do">
               <c:out value="${editIcon}" escapeXml="false" />
          </html:link>
          <html:link paramName="category" paramProperty="oid"
            paramId="categoryOid" page="/em/segment/segmentCategoryDelete.do"
            onclick="return confirmCategoryDelete(1);">
              <c:out value="${deleteIcon}" escapeXml="false" />
          </html:link>
        </td>
      </tr>
    </c:forEach>
    <!-- End category list row -->
  </table>
</html:form>
<c:if test="${not empty categoryList}">
  <jsp:include page="/mc/common/pager2.jsp" />
</c:if>